package com.mytest.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mytest.dao.impl.StudentDaoImpl;
import com.mytest.dao.interfaces.StudentDao;
import com.mytest.entity.Student;
import com.mytest.service.interfaces.StudentService;

@Service("studentService")
public class StudentServiceImpl implements StudentService {
	
	@Autowired
	private StudentDao studentDao ;

	@Override
	public Map<String, Object> selectOne(int id) throws Exception {
		return studentDao.selectOne(id);
	}

	@Override
	public List<Map<String, Object>> selectList(Map<String, Object> paramsMap) throws Exception {
		StudentDao studentDao = new StudentDaoImpl();
		Student params = new Student();
		for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
			if (entry.getKey().equals("id")) {
				params.setId((Integer) entry.getValue());
			} else if (entry.getKey().equals("name")) {
				params.setName((String) entry.getValue());
			} else if (entry.getKey().equals("sex")) {
				params.setSex((String) entry.getValue());
			} else if (entry.getKey().equals("age")) {
				params.setAge((Integer) entry.getValue());
			}
		}

		return studentDao.selectList(params);
	}

	@Override
	public int insert(Map<String, Object> paramsMap) throws Exception {
		StudentDao studentDao = new StudentDaoImpl();
		Student params = new Student();
		for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
			if (entry.getKey().equals("id")) {
				params.setId((Integer) entry.getValue());
			} else if (entry.getKey().equals("name")) {
				params.setName((String) entry.getValue());
			} else if (entry.getKey().equals("sex")) {
				params.setSex((String) entry.getValue());
			} else if (entry.getKey().equals("age")) {
				params.setAge((Integer) entry.getValue());
			}
		}
		return studentDao.insert(params);
	}

	@Override
	public int update(Map<String, Object> paramsMap) throws Exception {
		StudentDao studentDao = new StudentDaoImpl();
		Student params = new Student();
		for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
			if (entry.getKey().equals("id")) {
				params.setId((Integer) entry.getValue());
			} else if (entry.getKey().equals("name")) {
				params.setName((String) entry.getValue());
			} else if (entry.getKey().equals("sex")) {
				params.setSex((String) entry.getValue());
			} else if (entry.getKey().equals("age")) {
				params.setAge((Integer) entry.getValue());
			}
		}
		return studentDao.update(params);
	}

	@Override
	public int delete(Map<String, Object> paramsMap) throws Exception {
		StudentDao studentDao = new StudentDaoImpl();
		return studentDao.delete(paramsMap);
	}

}
